package com.cloudbroker.bcs.platform.dav.middle.bizservice.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cloudbroker.bcs.common.entity.HSBCPSBackVO;
import com.cloudbroker.bcs.common.util.*;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.HSBCPSErrorCodes;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.type.InvestCheckType;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.customer.back.InvestAccountListBackForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.customer.vo.InvestAccountForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.customer.vo.InvestAccountListForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.request.UserInfoForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.operate.InvestListBizService;
import com.cloudbroker.bcs.platform.dav.bizapi.service.operate.result.*;
import com.cloudbroker.bcs.platform.dav.bizapi.service.operate.vo.*;
import com.cloudbroker.bcs.platform.dav.bizapi.service.viewpoint.vo.BrokerInfoPlatForm;
import com.cloudbroker.bcs.platform.dav.bizapi.t2service.export.YunxinT2BizService;
import com.cloudbroker.bcs.platform.dav.common.config.ConfigValues;
import com.cloudbroker.bcs.platform.dav.common.util.SessionHelper;
import com.cloudbroker.bcs.platform.dav.middle.entity.Broker;
import com.cloudbroker.bcs.platform.dav.middle.entity.BrokerCheck;
import com.cloudbroker.bcs.platform.dav.middle.entity.BrokerManager;
import com.cloudbroker.bcs.platform.dav.middle.entity.Tenant;
import com.cloudbroker.bcs.platform.dav.middle.entityview.InvestCheckListView;
import com.cloudbroker.bcs.platform.dav.middle.entityview.InvestList;
import com.cloudbroker.bcs.platform.dav.middle.model.CustomerCache;
import com.cloudbroker.bcs.platform.dav.middle.service.*;
import com.cloudbroker.bcs.platform.dav.middle.util.ActivityUtil;
import com.cloudbroker.bsc.common.search.util.SearchTask;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by lnq on 2016/8/18.
 */
@Service
public class InvestListT2BizServiceImpl implements InvestListBizService{

    @Resource
    private TenantService tenantService;

    @Resource
    private BrokerService brokerService;

    @Resource
    private YunxinT2BizService yunxinT2;

    @Resource
    private CustomerService customerService;

    @Resource
    private BrokerManagerService brokerManagerService;

    @Resource
    private BrokerCheckService brokerCheckService;

    @Override
    public InvestCheckListResult funcInvestCheckList(InvestCheckListForm form) {
        boolean manager = managerJudge(form.getBranch_no());
        String keyword = form.getKeyword();
        Integer pageNo = ValueUtil.getInt(form.getPage_no(), 1);
        Integer pageSize = ValueUtil.getInt(form.getPage_count(), ConfigValues.getRequestNum());
        Integer stateType = ValueUtil.getInt(form.getState_type(),0);
        Integer checkFlag = null;
        if (!StringUtil.isBlank(form.getCheck_flag())) {
            checkFlag = ValueUtil.getInt(form.getCheck_flag());
        }
        Date startTime = StringUtil.isBlank(form.getStart_time()) ? null : DateTimeUtil.toDateOrNull(
                form.getStart_time(), DateTimeUtil.FORMAT_YYYYMMDD);
        Date endTime = StringUtil.isBlank(form.getEnd_time()) ? null : DateTimeUtil.toDateOrNull(
                form.getEnd_time(), DateTimeUtil.FORMAT_YYYYMMDD);
        List<Long> tenantIDs =  tenantService.getTenantIdsByBranchNo(form.getBranch_no());

        if (tenantIDs.isEmpty()) {
            if (manager) {
                tenantIDs = null;
            } else {
                return VOUtil.genErrorResult(HSBCPSErrorCodes.ClientErr.TENANT_NOT_EXIST,
                        InvestCheckListResult.class);
            }
        }

        List<Tenant> tenants = tenantService.gets(tenantIDs);
        List<Long> oneCheck = new ArrayList<>();
        List<Long> twoCheck = new ArrayList<>();
        for (Tenant tenant:tenants) {
            if (tenant.getBranch_grade() != null && tenant.getBranch_grade().equals("1")) {
                oneCheck.add(ValueUtil.getLong(tenant.getTenant_id()));
            } else if (tenant.getBranch_grade() != null && tenant.getBranch_grade().equals("2")) {
                twoCheck.add(ValueUtil.getLong(tenant.getTenant_id()));
            }
        }

        if (oneCheck.isEmpty()) {
            oneCheck.add(0L);
        }
        if (twoCheck.isEmpty()) {
            twoCheck.add(0L);
        }

        List<InvestCheckListView> dataList = null;
        Integer recordCount = 0;
        List<InvestCheckListData> resultList = new ArrayList<>();
        InvestCheckListResult result = new InvestCheckListResult();

        //stateType为0表示展示未审核列表，为1表示展示已审核列表
        if (stateType == 0){
            if (manager) {
                recordCount = brokerService.getWaitCheckCountsManager(keyword, oneCheck, twoCheck, startTime, endTime);
                dataList = brokerService.getWaitCheckListManager(keyword, oneCheck, twoCheck, startTime, endTime, (pageNo-1) * pageSize, pageSize);
            } else {
                recordCount = brokerService.getWaitCheckCounts(keyword, twoCheck, startTime, endTime);
                dataList = brokerService.getWaitCheckList(keyword, twoCheck, startTime, endTime, (pageNo-1) * pageSize, pageSize);
            }
        }
        if (stateType == 1) {
            if (manager) {
                recordCount = brokerService.getCheckenCountsManager(keyword, oneCheck, twoCheck, checkFlag, startTime, endTime);
                dataList = brokerService.getCheckenListManager(keyword, oneCheck, twoCheck, checkFlag, startTime, endTime, (pageNo-1) * pageSize, pageSize);
            } else {
                recordCount = brokerService.getCheckenCounts(keyword, oneCheck, twoCheck, checkFlag, startTime, endTime);
                dataList = brokerService.getCheckenList(keyword, oneCheck, twoCheck, checkFlag, startTime, endTime, (pageNo-1) * pageSize, pageSize);
            }
        }

        if (dataList !=null && !dataList.isEmpty()) {
            listResult(dataList, resultList);
        }

        result.setPage_no(pageNo);
        result.setRecord_count(recordCount);
        result.setData_list(resultList);
        return VOUtil.setSuccessResult(result);
    }

    private void listResult(List<InvestCheckListView> dataList, List<InvestCheckListData> resultList) {
        for (InvestCheckListView investCheckListView: dataList) {
            InvestCheckListData investCheckListData = new InvestCheckListData();
            investCheckListData.setBroker_id(investCheckListView.getBroker_id());
            investCheckListData.setBroker_name(investCheckListView.getBroker_name());
            investCheckListData.setMobile(investCheckListView.getMobile());
            String createTime = investCheckListView.getCreate_time();
            investCheckListData.setCreate_time(createTime.substring(0, createTime.length()-2));
            investCheckListData.setCheck_num(investCheckListView.getCheck_num());
            investCheckListData.setCheck_flag(investCheckListView.getCheck_flag());
            investCheckListData.setTenant_id(investCheckListView.getTenant_id());
            resultList.add(investCheckListData);
        }
    }

    @Override
    public HSBCPSBackVO funcInvestCheckProcess(InvestCheckProcessForm form) {
        String branchNo = form.getBranch_no();
        Integer branchGrade = ValueUtil.getInt(form.getBranch_grade());
        tenantService.updateByBranchNo(branchNo, branchGrade);
        return VOUtil.genSuccessBackVo();
    }

    @Override
    public HSBCPSBackVO funcInvestCheck(InvestCheckForm form) {
        boolean manager = managerJudge(form.getBranch_no());
        Integer tenantId = ValueUtil.getInt(form.getTenant_id());
        Long brokerId = ValueUtil.getLong(form.getBroker_id());
        Integer checkFlag = ValueUtil.getInt(form.getCheck_flag());
        BrokerCheck brokerCheck = null;

        List<BrokerCheck> brokerChecks = brokerCheckService.getBroker(brokerId, tenantId);
        if (brokerChecks == null || brokerChecks.isEmpty()) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.OperateErr.OPERATE_EXCEED_POWER_ERROR, HSBCPSBackVO.class);
        }

        if (!checkFlag.equals(1) && !checkFlag.equals(2)) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.OperateErr.OPERATE_CHECK_INFO_ERROR, HSBCPSBackVO.class);
        }

        Tenant tenant = tenantService.get(ValueUtil.getString(tenantId));
        if (tenant.getBranch_grade().equals("1")) {
            if (brokerChecks.size() != 1 || !brokerChecks.get(0).getCheck_manager().equals(1) ||
                    !brokerChecks.get(0).getCheck_result().equals("0") || !manager) {
                return VOUtil.genErrorResult(HSBCPSErrorCodes.OperateErr.OPERATE_EXCEED_POWER_ERROR, HSBCPSBackVO.class);
            }
            brokerCheck = brokerChecks.get(0);
            brokerCheck.setCheck_result(ValueUtil.getString(checkFlag));
            brokerCheck.setCheck_remark(form.getCheck_remark());
            brokerCheck.setCheck_time(new Date());
            brokerCheckService.updateCAS(brokerCheck);
            if (checkFlag.equals(1)) {
                Broker broker = brokerService.getById(brokerId, tenantId);
                broker.setReply_status("1");
                brokerService.updateCAS(broker);
            }
            if (checkFlag.equals(2)) {
                Broker broker = brokerService.getById(brokerId, tenantId);
                broker.setReply_status("2");
                brokerService.updateCAS(broker);
            }
        }
        if (tenant.getBranch_grade().equals("2")) {
            if (manager) {
                if (brokerChecks.size() != 2 || !brokerChecks.get(0).getCheck_result().equals("1")) {
                    return VOUtil.genErrorResult(HSBCPSErrorCodes.OperateErr.OPERATE_EXCEED_POWER_ERROR, HSBCPSBackVO.class);
                }
                brokerCheck = brokerChecks.get(1);
                brokerCheck.setCheck_result(ValueUtil.getString(checkFlag));
                brokerCheck.setCheck_remark(form.getCheck_remark());
                brokerCheck.setCheck_time(new Date());
                brokerCheckService.updateCAS(brokerCheck);
                if (checkFlag.equals(1)) {
                    Broker broker = brokerService.getById(brokerId, tenantId);
                    broker.setReply_status("1");
                    brokerService.updateCAS(broker);
                }
                if (checkFlag.equals(2)) {
                    Broker broker = brokerService.getById(brokerId, tenantId);
                    broker.setReply_status("2");
                    brokerService.updateCAS(broker);
                }
            } else {
                if (brokerChecks.size() != 1 || !brokerChecks.get(0).getCheck_manager().equals(1) ||
                        !brokerChecks.get(0).getCheck_result().equals("0")) {
                    return VOUtil.genErrorResult(HSBCPSErrorCodes.OperateErr.OPERATE_EXCEED_POWER_ERROR, HSBCPSBackVO.class);
                }
                brokerCheck = brokerChecks.get(0);
                brokerCheck.setCheck_result(ValueUtil.getString(checkFlag));
                brokerCheck.setCheck_remark(form.getCheck_remark());
                brokerCheck.setCheck_time(new Date());
                brokerCheckService.updateCAS(brokerCheck);
                if (checkFlag.equals(1)) {
                    BrokerCheck check = new BrokerCheck();
                    check.setBroker_id(brokerCheck.getBroker_id());
                    check.setCheck_manager(2);
                    check.setCheck_result("0");
                    check.setTenant_id(brokerCheck.getTenant_id());
                    brokerCheckService.save(check);
                } else {
                    Broker broker = brokerService.getById(brokerId, tenantId);
                    broker.setReply_status("2");
                    brokerService.updateCAS(broker);
                }
            }
        }
        return VOUtil.genSuccessBackVo();
    }

    @Override
    public InvestAccountListBackForm funcInvestAccountList(InvestAccountForm param) {
        Integer page_no = ValueUtil.getInt(param.getPage_no(), 1);
        Integer page_size = ValueUtil.getInt(param.getPage_count(),ConfigValues.getRequestNum());
        Integer state_type = ValueUtil.getInt(param.getState_type(),0);
        List<InvestList> dataList =  tenantService.getTenantListByBranchNo(param.getBranch_no());
        List<Long> tenant_ids = new ArrayList<Long>();
        for(InvestList investList : dataList){
           tenant_ids.add(ValueUtil.getLong(investList.getTenant_id()));
        }
        Integer record_count = 0;
        if(tenant_ids.size() == 0){
            tenant_ids.add(0,0L);
        }
        if(state_type == 0){
            record_count = brokerService.getInvestCounts(param.getKeyword(),tenant_ids);
        }else{
            record_count = brokerService.getInvestBanCounts(param.getKeyword(),tenant_ids);
        }
        List<InvestList> investLists = null;
        Integer limit_page_no = 0;
        if ((ValueUtil.getInt(param.getPage_no()) - 1) * page_size >= record_count) {
            investLists = new ArrayList<>();
        }else{
            if (page_no <= 0) {
                page_no = 1;
            }
            limit_page_no = (page_no - 1) * page_size;
            if(state_type == 0){
                investLists = brokerService.getInvestList(param.getKeyword(),limit_page_no,page_size,tenant_ids);
            }else{
                investLists = brokerService.getInvestBanList(param.getKeyword(),limit_page_no,page_size,tenant_ids);
            }
        }
        if(investLists == null ){
            investLists = new ArrayList<InvestList>();
        }
        InvestAccountListBackForm result = new InvestAccountListBackForm();
        List<InvestAccountListForm> investAccountListForms = new ArrayList<>();
        for(InvestList investList: investLists){
            InvestAccountListForm investAccountListForm = new InvestAccountListForm();
            investAccountListForm.setBroker_id(investList.getBroker_id());
            investAccountListForm.setBroker_name(investList.getBroker_name());
            investAccountListForm.setMobile(investList.getMobile());
            investAccountListForm.setWx_public_account(investList.getWx_public_account());
            investAccountListForm.setBranch_name(investList.getBranch_name());
            investAccountListForm.setCreate_date(investList.getCreate_date());
            investAccountListForm.setTenant_id(investList.getTenant_id());
            if(state_type == 1){
                Date frozen_start = investList.getFrozen_start();
                Date frozen_end = investList.getFrozen_end();
                Integer TimeDiff = 0;
                if(DateTimeUtil.isInDate(frozen_start,frozen_end)){
                    TimeDiff = DateTimeUtil.getDateDiffer(new Date(),frozen_end,true);
                }else {
                    TimeDiff = DateTimeUtil.getDateDiffer(frozen_start, frozen_end, true);
                }

                    String frozen_date = TimeDiff >= 72000 ? "永久" : TimeDiff.toString()+"天";
                    investAccountListForm.setFrozen_date(frozen_date);
            }
            investAccountListForms.add(investAccountListForm);
        }
        result.setPage_no(page_no);
        result.setRecord_count(record_count);
        result.setData_list(investAccountListForms);
        return VOUtil.setSuccessResult(result);
    }

    @Override
    public HSBCPSBackVO funcOperateInvestList(BrokerFundFrozenForm form) {
        String operate = form.getOperate();
        JSONArray jsonArray = JSONUtil.toJsonArray(operate);
        List<InvestList> investLists = new ArrayList<>();
        String frozen_start = jsonArray.getJSONObject(0).getString("frozen_start");
        String frozen_end = jsonArray.getJSONObject(0).getString("frozen_end");
        Date start = DateTimeUtil.toDateOrNull(frozen_start,DateTimeUtil.FORMAT_YYYYMMDDHHMMSS);
        Date end = DateTimeUtil.toDateOrNull(frozen_end,DateTimeUtil.FORMAT_YYYYMMDDHHMMSS);
        if((!"".equals(frozen_start)||!"".equals(frozen_end))&&(start == null || end == null)){
            return VOUtil.genErrorResult(HSBCPSErrorCodes.OperateErr.DATE_PATTEN_ERROR,HSBCPSBackVO.class);
        }
        if(!"".equals(frozen_start)&&!"".equals(frozen_end)&&start.getTime() - end.getTime() >=0){
                return VOUtil.genErrorResult(HSBCPSErrorCodes.OperateErr.DATE_INFO_ERROR,HSBCPSBackVO.class);
        }
        if("".equals(frozen_start) && "".equals(frozen_end)){
            frozen_start = null;
            frozen_end = null;
        }
        for(int i=0;i<jsonArray.size();i++){
            InvestList investList = new InvestList();
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            investList.setTenant_id(jsonObject.getInteger("tenant_id"));
            investList.setBroker_id(jsonObject.getLong("broker_id"));
            investLists.add(investList);
        }
        Integer SpecifyInvestCounts = brokerService.getSpecifyInvestCounts(investLists);
        if(jsonArray.size() != SpecifyInvestCounts){
            return VOUtil.genErrorResult(HSBCPSErrorCodes.OperateErr.INVEST_NOT_EXIST_ERROR,HSBCPSBackVO.class);
        }
        brokerService.operateInvestList(frozen_start,frozen_end,investLists);
        HSBCPSBackVO result = new HSBCPSBackVO();
        return VOUtil.setSuccessResult(result);
    }

    @Override
    public InvestBranchNames funcGetBranchNames(BranchNoForm form) {
        List<Long> tenant_ids = tenantService.getTenantIdsByBranchNo(form.getBranch_no());
        List<String> investBranchNames = tenantService.funcGetBranchNames(tenant_ids);
        InvestBranchNames result = new InvestBranchNames();
        result.setBranch_names(investBranchNames);
        return VOUtil.setSuccessResult(result);
    }

    @Override
    public InvestDetailsForm funcGetInvestDetails(InvestBorkerIdForm form) {
        Integer tenant_id = ValueUtil.getInt(form.getTenant_id());
        Integer broker_id = ValueUtil.getInt(form.getBroker_id());
        InvestDetailsForm result = brokerService.funcGetInvestDetails(broker_id,tenant_id);
        return VOUtil.setSuccessResult(result);
    }

    @Override
    public InvestCheckDetailsForm funcInvestCheckDetails(InvestCheckBrokerForm form) {
        Integer tenant_id = ValueUtil.getInt(form.getTenant_id());
        Integer broker_id = ValueUtil.getInt(form.getBroker_id());
        Integer check_type = ValueUtil.getInt(form.getCheck_type());
        InvestCheckDetailsForm result = new InvestCheckDetailsForm();
        //0:待审核列表投顾的详情，1：已审核列表投顾的详情
        if(check_type == 0){
            result = brokerService.funcInvestNotCheckInvestDetails(broker_id,tenant_id);
        }
        if(check_type == 1){
            result = brokerService.funcInvestCheckDetails(broker_id,tenant_id);
        }
        if(check_type == 1){
            Integer branch_grade = result.getBranch_grade();
            Integer check_manager = result.getCheck_manager();
            Integer check_result = ValueUtil.getInt(result.getCheck_result());
            if(branch_grade.equals(check_manager) && check_result.equals(1)){
                result.setCheck_pass(InvestCheckType.PASS.getVal());
            }else{
                result.setCheck_pass(InvestCheckType.FAIL.getVal());
            }
        }
        result.setTenant_id(form.getTenant_id());
        return VOUtil.setSuccessResult(result);
    }

    @Override
    public HSBCPSBackVO funcInvestChangeInfo(BrokerInfoPlatForm form) {
        Tenant tenant = tenantService.getByCompanyId(ValueUtil.getInt(form.getCompany_id()));
        if (tenant == null) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ClientErr.TENANT_NOT_EXIST, HSBCPSBackVO.class);
        }
        if (!StringUtil.isEmpty(form.getBranch_no())) {
            if (!tenant.getBranch_no().equals(form.getBranch_no())) {
                return VOUtil.genErrorResult(HSBCPSErrorCodes.ClientErr.TENANT_NOT_EXIST, HSBCPSBackVO.class);
            }
        }
        BrokerManager brokerManager = brokerManagerService.getByUserId(ValueUtil.getLong(form.getUser_id()));
        if (brokerManager == null || !brokerManager.getTenant_id().equals(tenant.getTenant_id())) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ClientErr.BROKER_NOT_FIND, HSBCPSBackVO.class);
        }
        Broker broker = brokerService.getById(brokerManager.getBroker_id(), tenant.getTenant_id());
        Map<String, Object> jsonObject = JSONObject.parseObject(JSONObject.toJSONString(form));
        String userCenterKeyKey = ConfigValues.getUserCenterKey();
        boolean validate = ActivityUtil.validateHashedQueryString(jsonObject, userCenterKeyKey);
        if (!validate) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ClientErr.VALIDATE_FAIL, HSBCPSBackVO.class);
        }

        String broker_name = form.getBroker_name();
        if (!StringUtil.isEmpty(broker_name) && !broker.getBroker_name().equals(broker_name)) {
            if (checkUserName(broker_name, tenant.getTenant_id())) {
                return VOUtil.genErrorResultVo(HSBCPSErrorCodes.ClientErr.USER_NAME_EXSIT);
            }
        }
        String mobile = form.getMobile();
        if (!StringUtil.isEmpty(mobile) && !broker.getMobile().equals(mobile)) {
            if (checkMobile(mobile, tenant.getTenant_id())) {
                return VOUtil.genErrorResultVo(HSBCPSErrorCodes.ClientErr.MOBILE_REGISTERED);
            }
        }
        String customer_profile = form.getProfile();

        //云信更新用户名片
        if (!StringUtil.isEmpty(customer_profile)) {
            broker.setProfile(customer_profile);
        }
        if (!StringUtil.isEmpty(broker_name)) {
            broker.setBroker_name(broker_name);
            brokerManager.setName(broker_name);
        }
        if (!StringUtil.isEmpty(mobile)) {
            broker.setMobile(mobile);
            brokerManager.setMobile(mobile);
        }
        UserInfoForm info = new UserInfoForm();
        info.setAccid(brokerManager.getYunxin_acct());
        info.setSign(broker.getProfile());
        info.setName(broker.getBroker_name());
        yunxinT2.updateUserInfo(info);
        /*更新一下brokermanager*/
        brokerManagerService.update(brokerManager);

        if (!StringUtil.isEmpty(form.getApplicant_name())) {
            broker.setApplicant_name(form.getApplicant_name());
        }
        if (!StringUtil.isEmpty(form.getCompany_name())) {
            broker.setCompany(form.getCompany_name());
        }
        if (!StringUtil.isEmpty(form.getId_no())) {
            broker.setId_no(form.getId_no());
        }
        if (!StringUtil.isEmpty(form.getWx_public_account())) {
            broker.setWx_public_account(form.getWx_public_account());
        }
        if (!StringUtil.isEmpty(form.getWx_account())) {
            broker.setWx_account(form.getWx_account());
        }
        if (!StringUtil.isEmpty(form.getWeibo_id())) {
            broker.setWeibo_id(form.getWeibo_id());
        }
        if (!StringUtil.isEmpty(form.getQq())) {
            broker.setQq(form.getQq());
        }
        if (!StringUtil.isEmpty(form.getFr_code())) {
            broker.setFr_code(form.getFr_code());
        }
        if (!StringUtil.isEmpty(form.getOwn_organ())) {
            broker.setOwn_organ(form.getOwn_organ());
        }
        if (!StringUtil.isEmpty(form.getCertificate_id())) {
            broker.setCertificate_id(form.getCertificate_id());
        }
        if (!StringUtil.isEmpty(form.getCertificate_type())) {
            broker.setCertificate_type(form.getCertificate_type());
        }
        if (!StringUtil.isEmpty(form.getEmail())) {
            broker.setEmail(form.getEmail());
        }
        if (!StringUtil.isEmpty(form.getAccount_holder())) {
            broker.setAccount_holder(form.getAccount_holder());
        }
        if (!StringUtil.isEmpty(form.getBank_name())) {
            broker.setBank_name(form.getBank_name());
        }
        if (!StringUtil.isEmpty(form.getBank_account())) {
            broker.setBank_account(form.getBank_account());
        }
        if (!StringUtil.isEmpty(form.getProvince_name())) {
            broker.setProvince_name(form.getProvince_name());
        }
        if (!StringUtil.isEmpty(form.getCity_name())) {
            broker.setCity_name(form.getCity_name());
        }
        broker.setUpdate_time(new Date());
        brokerService.update(broker);
        Map<String, String> mapUpdate = new HashMap<String, String>();
        mapUpdate.put("broker_name", broker.getBroker_name());
        Map<String, String> mapQry = new HashMap<String, String>();
        mapQry.put("broker_id", "es equals " + broker.getBroker_id());
        SearchTask.updateBitchTask(mapQry, mapUpdate);
        CustomerCache cache = SessionHelper.getInstance().fetchSessionIdentity(CustomerCache.class);
        if (cache != null) {
            cache.setBroker(broker);
            SessionHelper.getInstance().renewIdentity(cache);
        }
        return VOUtil.genSuccessBackVo();
    }

    private boolean managerJudge(String branchNo) {
        String[] branchSplit = branchNo.split(",");
        for (int i = 0; i < branchSplit.length; i++) {
            if (ConfigValues.getYunjiBranchNo().equals(branchSplit[i])) {
                return true;
            }
        }
        return false;
    }

    //检测该租户下面是否已经有昵称，有返回true,没有返回false
    private boolean checkUserName(String name, Integer tenant_id) {
        Integer total = brokerManagerService.checkUserNameByTenant(name,tenant_id) + customerService.checkUserNameByTenant(name,tenant_id);
        if (total > 0) {
            return true;
        }
        return false;
    }

    //检测该租户下面是否已经有该手机号，有返回true,没有返回false
    private boolean checkMobile(String mobile, Integer tenant_id) {
        if (brokerManagerService.checkMobileByTenant(mobile,tenant_id) > 0) {
            return true;
        }
        if (customerService.checkMobileByTenant(mobile,tenant_id) > 0) {
            return true;
        }
        return false;
    }
}